package org.ovhoo.zonfig;

public class Value {
	private Type.typesEnum type;
	private long longValue;
	private double doubleValue;
	private String stringValue = "";
	
	
	/**
	 * Constructor with value as a double
	 * @param type : the type of the value
	 * @param value : the value as double
	 */
	Value(Type.typesEnum type, double value){
		this.type = type;
		this.setValue(value);
		
	}
	
	/**
	 * Constructor with value as a long
	 * @param type : the type of the value
	 * @param value : the value as long
	 */
	Value(Type.typesEnum type, long value){
		this.type = type;
		this.setValue(value);
		
	}
	
	/**
	 * Constructor with value as a int
	 * @param type : the type of the value
	 * @param value : the value as int
	 */
	Value(Type.typesEnum type, int value){
		this.type = type;
		this.setValue(value);
		
	}
	
	/**
	 * Constructor with value as a string
	 * @param type : the type of the value
	 * @param value : the value as string
	 */
	Value(Type.typesEnum type, String value){
		this.type = type;
		this.setValue(value);
		
	}
	
	/**
	 * Set the value by giving a string
	 * @param value : the new value as string
	 */
	public void setValue(String value){
		//avoid the case of a null string
		if (value == null){
			return;
		}
		
		switch(this.type){
		case floatType : 	this.doubleValue = Double.valueOf(value);
							this.longValue = (long)this.doubleValue;
							this.stringValue = Double.toString(this.doubleValue);
							break;
							
		case intType : 		this.longValue = Long.valueOf(value);
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
						
		case unsigned : 	this.longValue = Math.abs(Long.valueOf(value));
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
						
		case string : 		this.stringValue = value;
							this.longValue = Long.valueOf(value);
							this.doubleValue = Double.valueOf(value);
							break;
						
		default : break;
		}
	}
	
	/**
	 * Set the value by giving a double
	 * @param value : the new value as double
	 */
	public void setValue(double value){
		//avoid the case of a null string
		
		switch(this.type){
		case floatType : 	this.doubleValue = value;
							this.longValue = (long)this.doubleValue;
							this.stringValue = Double.toString(this.doubleValue);
							break;
							
		case intType : 		this.longValue = (long)value;
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
						
		case unsigned : 	this.longValue = Math.abs((long)value);
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
		
		case string : 		this.stringValue = Double.toString(value);
							this.longValue = Long.valueOf(this.stringValue);
							this.doubleValue = Double.valueOf(this.stringValue);
							break;
						
		default : break;
		}
	}
	
	
	/**
	 * Set the value by giving a long
	 * @param value : the new value as long
	 */
	public void setValue(long value){
		//avoid the case of a null string
		
		switch(this.type){
		case floatType : 	this.doubleValue = (double)value;
							this.longValue = (long)this.doubleValue;
							this.stringValue = Double.toString(this.doubleValue);
							break;
							
		case intType : 		this.longValue = value;
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
						
		case unsigned : 	this.longValue = Math.abs(value);
							this.doubleValue = (double)this.longValue;
							this.stringValue = Long.toString(this.longValue);
							break;
							
		case string : 		this.stringValue = Long.toString(value);
							this.longValue = Long.valueOf(this.stringValue);
							this.doubleValue = Double.valueOf(this.stringValue);
							break;
		default : break;
		}
	}
	
	/**
	 * Set the value by giving a int
	 * @param value : the new value as int
	 */
	public void setValue(int value){
		this.setValue((long)value);
	}
	
	//geters
	
	/**
	 * return the value as long
	 * @return the value as long
	 */
	public long getLongValue(){
		return this.longValue;
	}
	
	/**
	 * return the value as int
	 * @return the value as int
	 */
	public int getIntValue(){
		return (int)this.longValue;
	}
	
	/**
	 * return the value as double
	 * @return the value as double
	 */
	public double getDoubleValue(){
		return this.doubleValue;
	}
	
	/**
	 * return the value as string
	 * @return the value as string
	 */
	public String getStringValue(){
		return stringValue;
	}
	
	/**
	 * return the value as string
	 * @return the value as string
	 */
	public String toString(){
		return getStringValue();
	}

	
	
	

}
